package com.wskj.common.utils.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wskj.common.utils.common.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class SenInfoCheckUtils {

    private static Logger logger = LoggerFactory.getLogger(SenInfoCheckUtils.class);

    /**
     * 获取Token  小程序
     *
     * @param
     * @param
     * @return AccessToken
     */
    public static String getMpToken(String appId, String appSecret) {
        return getMpAccessToken(appId, appSecret);
    }

    public static void sendMsg(String wxId,String templateId,String page,String type, List<String> msgList, String appId, String appSecret) {
        String mpToken = getMpToken(appId, appSecret);
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + mpToken;
        // 拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(wxId);//用户的openid（要发送给那个用户，通常这里应该动态传进来的）
        wxMssVo.setTemplate_id(templateId);//订阅消息模板id
        // 退款、用户未读、商家未读 都跳转到消息页面
        wxMssVo.setPage(page);
        wxMssVo.setData(getParam(type,msgList));
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, wxMssVo, String.class);
        String body = responseEntity.getBody();
        logger.info("=========消息模板=========" + templateId);
        logger.info("=========订阅发送消息后结果=========" + body);
    }

    /**
     * 获取access_token
     * 每个两个小时自动刷新AcessTocken
     */

    public static String getMpAccessToken(String appId, String appSecret) {
        String jsonResult = HttpClientUtil.doPost("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret);
        JSONObject parseObject = JSON.parseObject(jsonResult);
        logger.info("=========accessTokenOut=========" + parseObject.toJSONString());

        String errcode = parseObject.getString("errcode");
        String accessToken = parseObject.getString("access_token");
        String expiresIn = parseObject.getString("expires_in");
        return accessToken;
    }

    public static Map<String, TemplateParam> getParam(String type, List<String> msgList) {
        Map<String, TemplateParam> paras = new HashMap<>(3);
        // type 1:订单支付成功通知 2:退款通知
        if ("1".equals(type)) {
            // 订单编号
            paras.put("character_string1", new TemplateParam(msgList.get(0)));
            // 支付时间
            paras.put("date2", new TemplateParam(msgList.get(1)));
            // 订单金额
            paras.put("amount3", new TemplateParam(msgList.get(2)));
            // 商品名称
            paras.put("thing4", new TemplateParam(msgList.get(3)));
            // 支付人
            paras.put("thing6", new TemplateParam(msgList.get(4)));
        }else if("2".equals(type)){
            // 订单编号
            paras.put("character_string1", new TemplateParam(msgList.get(0)));
            // 退款金额
            paras.put("amount2", new TemplateParam(msgList.get(1)));
            // 退款时间
            paras.put("time3", new TemplateParam(msgList.get(2)));
            // 退款事由
            paras.put("thing4", new TemplateParam(msgList.get(3)));
            // 姓名
            paras.put("thing5", new TemplateParam(msgList.get(4)));
        }
        return paras;
    }
}
